const { SuccessModel, ErrorModel } = require("../../model/resModel");
const { getList, createNewBanner, deleteBanner } = require('../../controllers/admin/banner');

const handleBannerRoute = (req, res) => {
    const method = req.method;
    const id = req.query.id;
    const bannerData = req.body;

    //新建轮播图
    if (method === 'POST' && req.path === '/api/banner/new') {
        const newBannerDataPromise = createNewBanner(bannerData);
        return newBannerDataPromise.then(newBannerData => {
            return new SuccessModel(newBannerData,'新建轮播图成功');
        })
    }

    //删除轮播图
    if (method === 'POST' && req.path === '/api/banner/delete') {
        const deletedBannerPromise = deleteBanner(id);
        return deletedBannerPromise.then(deletedBannerData => {
            if (deletedBannerData) {
                return new SuccessModel(`删除轮播图${id}成功`);
            } else {
                return new ErrorModel('删除失败');
            }
        })
    }


    //获取轮播图列表
    if (method === 'GET' && req.path === '/api/banner/list') {
        const listDataPromise = getList();
        return listDataPromise.then(listData => {
            
            return new SuccessModel(listData,'轮播图列表');
        });
    }
}

module.exports = handleBannerRoute;